<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-5-15
 * Time: 下午1:38
 */

namespace workin\modules\test\controllers;


use common\components\tools\Bank;
use common\components\tools\Tools;
use common\components\tools\Word;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use yii\helpers\FileHelper;
use yii\web\Controller;

class WordController extends Controller
{
    public function actionTest()
    {
        $images = [
            ['picid'=>'0001', 'picurl'=>"http://7xsbq6.com1.z0.glb.clouddn.com/m.jpg"],
            ['picid'=>'0002', 'picurl'=>"http://7xsbq6.com1.z0.glb.clouddn.com/m.jpg"],
            ['picid'=>'0003', 'picurl'=>"http://7xsbq6.com1.z0.glb.clouddn.com/m.jpg"],
            ['picid'=>'0004', 'picurl'=>"http://7xsbq6.com1.z0.glb.clouddn.com/m.jpg"],
        ];
        var_dump(serialize($images));exit;
        $pics = Bank::uploadIcbcPics0($images);
        var_dump($pics);
        exit;

        $image_url = "http://7xsbq6.com1.z0.glb.clouddn.com/m.jpg";
        $x = file_get_contents($image_url);
        if (is_dir(\Yii::getAlias('@runtime/files'))){}else{
            FileHelper::createDirectory(\Yii::getAlias('@runtime/files'), "0777");
        }
        file_put_contents(\Yii::getAlias('@runtime/files/x.jpg'), $x);

        $phpword = new PhpWord();
        $phpword_section = $phpword->createSection();

        $image_path = \Yii::getAlias("@runtime/y.png");
        $imageStyle = $this->word_img($image_path, 900);
        $phpword_section->addImage($image_path, $imageStyle);
        $phpword_section->addPageBreak();

        $image_path = \Yii::getAlias("@runtime/y.png");
        $imageStyle = $this->word_img($image_path, 450);
        $phpword_section->addImage($image_path, $imageStyle);

        $image_path = \Yii::getAlias("@runtime/y.png");
        $imageStyle = $this->word_img($image_path, 450);
        $phpword_section->addImage($image_path, $imageStyle);
        $phpword_section->addPageBreak();

        $objWriter = IOFactory::createWriter($phpword, 'Word2007');
        $doc_f = \Yii::getAlias('@runtime/z') .'.docx';
        $objWriter->save( $doc_f );
        $fileinfo['fileUrl']=$doc_f;
        return $this->render('test');
    }

    public function createCreditDOC($data, $path){
        $pw = new PHPWord();
        //var_dump($pw);
        $section = $pw->createSection();
        $buy_name = $data['cm_vip_name'];
        $_t = json_decode($data['cm_img'], true);
        $_tmp = array();
        foreach ($_t as $_tt){
            $_tmp[$_tt['card_name']] = $_tt;
        }
        $data['cm_img'] = $_tmp;
        $img_file = './'. $data['cm_img']['授权书']['card_url'];
        $imageStyle = $this->word_img($img_file, 900);
        $img_file = $this->word_img_zip($img_file, $imageStyle);
        $section->addImage( $img_file, $imageStyle );
        $section->addPageBreak();
        //$section->addText( $man['cm_type'] . ': ' . $buy_name . ' ' . $man['cm_card'] );
        $img1 = './'. $data['cm_img']['身份证正面']['card_url'];
        $imageStyle = $this->word_img($img1, 450);
        $img1 = $this->word_img_zip($img1, $imageStyle);
        $section->addImage( $img1, $imageStyle );
        $img2 = './'. $data['cm_img']['身份证反面']['card_url'];
        $imageStyle = $this->word_img($img2, 450);
        $img2 = $this->word_img_zip($img2, $imageStyle);
        $section->addImage( $img2, $imageStyle );
        if($path==LOG_PATH && $data['cm_img']['签字照']){
            $section->addPageBreak();
            $img_file = './'. $data['cm_img']['签字照']['card_url'];
            $imageStyle = $this->word_img($img_file, 900);
            $img_file = $this->word_img_zip($img_file, $imageStyle);
            $section->addImage( $img_file, $imageStyle );
        }
        $objWriter = IOFactory::createWriter($pw, 'Word2007');
        //$ffname = iconv("utf-8","gb2312",$data['cm_number'].'_'.$data['cm_type'].'_'.$buy_name);
        $ffname = iconv("utf-8", "gb2312", $buy_name.'_'.$data['cm_number']);
        if($path!=LOG_PATH) $ffname = $data['cm_card'];
        $doc_f = $path.$ffname .'.docx';
        $objWriter->save( $doc_f );
        $fileinfo['fileName']=$ffname;
        $fileinfo['fileUrl']=$doc_f;
        return $fileinfo;
    }

    function word_img($img, $h = 450){
        $w = 600;
        if (empty($img)) return null;
        $a = getimagesize($img);
        $s0 = $w/$h;
        $s1 = $a[0]/$a[1];
        if($s0>=$s1 && $a[1]>$h)
        {
            $d['height'] = $h;
            $d['width'] = $h/$a[1]*$a[0];
            return $d;
        }
        if($s0<$s1 && $a[0]>$w)
        {
            $d['width'] = $w;
            $d['height'] = $w/$a[0]*$a[1];
            return $d;
        }
        return null;
    }

    function  word_img_zip($img, $imageStyle){
        if($imageStyle == null)
            return $img;
        else{
            $inf = pathinfo($img);
            $ext = $inf['extension'];
            $nimg = str_replace('.'.$ext, '_s.'.$ext, $img);

            import("ORG.Util.Image");
            Image::thumb($img,$nimg,'',$imageStyle['width'],$imageStyle['height'],true);
            return $nimg;
        }
    }

    public function actionTest1()
    {
        $url = "http://ysjr.qiche100.com/Icbc/get_pic?imageName=1846646.jpg";
        var_dump(Word::getExt($url));
    }
}